//
//  NSObject+TimerSafe.m
//  CommonTool
//
//  Created by Name i on 2022/4/14.
//

#import "NSTimer+TimerSafe.h"
#import "NNTimerProxy.h"
#import "NSObject+Swizzling.h"

@implementation NSTimer (TimerSafe)

+ (void)timerExchangeMethod {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [NSTimer tnSwizzlingClassMethod: @selector(scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:)
                             withMethod: @selector(safe_scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:)
                              withClass: [self class]];
        
        [NSTimer tnSwizzlingClassMethod: @selector(scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:)
                              withMethod: @selector(safe_scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:)
                               withClass: [self class]];
    });
}

+ (NSTimer *)safe_timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo {
    return [self safe_timerWithTimeInterval:ti
                                     target:[NNTimerProxy proxyWithTarget:aTarget]
                                   selector:aSelector
                                   userInfo:userInfo
                                    repeats:yesOrNo];
}

+ (NSTimer *)safe_scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo {
    return [self safe_scheduledTimerWithTimeInterval:ti
                                              target:[NNTimerProxy proxyWithTarget:aTarget]
                                            selector:aSelector
                                            userInfo:userInfo
                                             repeats:yesOrNo];
}





@end
